<template>
  <el-form label-width="100px" :model="form" :rules="rules" ref="formRef">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name"></el-input>
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code"></el-input>
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width: 80%" placeholder="请选择">
        <el-option
          v-for="item in list"
          :key="item.id"
          :value="item.username"
          :label="item.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button @click="hCancel">取消</el-button>
      <el-button type="primary" @click="Hsubmit">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
// 调用api
import {
  getSimpleEmployee,
  addDepartments,
  getDepartDetail,
  updateDepartments,
} from "@/api/departments";

export default {
  // 父传子
  props: {
    pid: {
      // 类型
      typr: String,
      // 必传
      required: true,
    },
    isEdit: {
      // 类型
      typr: Boolean,
      // 必传
      required: true,
    },
    originList:{
      type:Array,
      required: true,
    }
  },
  data() {
    const validCode = (rule , value ,callback)=>{
      let codeList = this.originList.map(item =>item.code)
      console.log(codeList)
      if(this.isEdit){
        codeList = this.originList.filter(item => item.id !== this.pid).map(item =>item.code)
      }
      if(codeList.includes(value)){
        callback(new Error (value+'被占用了'))
      }else{
        callback()
      }
    }
    const validName = (rule , value ,callback)=>{
      
      let codeName = this.originList.map(item =>item.name)
      console.log(codeName);
      if (this.isEdit) {
        const item = this.originList.find(item => item.id === this.pid)
        const pid = item.pid
        codeName = this.originList.filter(item => item.pid === pid && item.id !== this.pid).map(item => item.name)
      }
      if(codeName.includes(value)){
        callback(new Error (value+'被占用了'))
      }else{
        callback()
      }
    }
    return {
      // 双向绑定
      form: {
        name: "",
        introduce: "",
        manager: "",
        code: "",
      },
      // 表单验证
      rules: {
        name: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
          {min: 1,max: 50,message: "部门名称要求1-50个字符",trigger: "blur"}, 
          { trigger: "blur" ,validator:validName}
        ],
        code: [
          { required: true, message: "部门编码不能为空", trigger: "blur" },
          {min: 1,max: 50,message: "部门编码要求1-50个字符",trigger: "blur"},
          {required: true, trigger: "blur" ,validator:validCode}
        ],
        manager: [
          { required: true, message: "部门负责人不能为空", trigger: "blur" },
        ],
        introduce: [
          { required: true, message: "部门介绍不能为空", trigger: "blur" },
          {min: 1,max: 300,message: "部门介绍要求1-300个字符",trigger: "blur"},
        ],
      },
      // 员工列表 v-for
      list: [],
    };
    
  },
  // 第一次自动加载发送ajax
  created() {
    // 获取员工简单列表方法
    this.loadSimpleEmployee();
    // 编辑部门
    this.loadDetail();
  },

  methods: {
    // 调用api
    async loadSimpleEmployee() {
      const res = await getSimpleEmployee();
      console.log("获取员工简单列表", res.data);
      this.list = res.data;
    },
    async doAdd() {
      try {
        // 确认按钮 发送api
        await addDepartments({ ...this.form, pid: this.pid }); //接口文档需要的数据
        this.$message.success("添加成功");
        // 子传父
        this.$emit("success");
      } catch (err) {
        console.log(err);
        this.$message.error("添加失败");
      }
    },
    // 编辑操作
    async doEdit() {
      try {
        await updateDepartments(this.form);
        this.$emit("success");
      } catch (err) {
        console.log(err);
      }
    },
    Hsubmit() {
     this.$refs.formRef.validate(valid => {
      if (valid) {
    	  this.isEdit ? this.doEdit() : this.doAdd()
      }
  })
    },
    async loadDetail() {
      if (this.isEdit) {
        // 编辑 发送ajax
        const res = await getDepartDetail(this.pid);
        console.log("编辑", res);
        // 数据回填
        this.form = res.data;
      }
    },
    // 子传父 点击取消按钮 关闭弹层
    hCancel() {
      this.$emit("close");
    },
  },
};
</script>


